<?php


class Custom{

	var $config;

	function __construct($file_path){
		try{
			$cnt = file_get_contents($file_path);
			$this->config = json_decode($cnt,true);
		}
		catch(Exception $e){
			exit;
		}
	}

	// function make_form($name,$format){
	// 	$config = $this->config[$name];

	// 	$struct = $config['struct'];
	// 	preg_replace_callback('/\{\{(.*?)\}\}/', function($matched){
	// 		$c = $config[trim($matched[1])];

	// 	}, $struct);
	// }



	function send($url,$type,$source){
		$param = array();
		foreach($this->config['config'] as $key => $val){
			$param[$key] = $val;
		}
		//time
		$param['datetime'] = date("YmdHis");
		//sign
		$sign = '';
		$sign .= $this->config['config']['v'];
		$sign .= $this->config['config']['ip'];
		$sign .= $this->config['config']['sessionKey'];
		$sign .= $param['datetime'];
		$sign .= $this->config['config']['appKey'];
		

		$sign = strtoupper(md5($sign));
		$param['sign'] = $sign;



		//json_obj
		$param['JSON_OBJ'] = ($this->get_json($type,$source));

		print_r($param);


		return curl_post($url,$param);
		return $param['JSON_OBJ'];
	}

	function get_form($type){
		$struct = $this->config[$type]['struct'];
		$arr = array();
		$_this = $this;
		preg_replace_callback("/\{\{\s*(.*?)\s*\}\}/",function($matched) use (&$arr,&$type,&$_this) {
			if(isset($_this->config[$type][$matched[1]])){
				foreach($_this->config[$type][$matched[1]] as $key => $val){
					$arr[$key] = $val;
				}
			}
		},$struct);
		return $arr;

	}

	function get_xml($type,$source){
		
	}

	function get_json($type,$source){
		$source_data = array();
		switch($type){
			case 'goods':
				// $source_data = 
				$m = Model();
				foreach ($source as $key => $val) {
					$ret = $m->cls()->table('goods')->where(array('goods_commonid' => $val))->find();
					$source_data[$key] = array();
					$source_data[$key]['goods'] = $ret;
					$store_info = $m->cls()->table('store')->where(array('store_id' => $ret['store_id']))->find();
					$source_data[$key]['store'] = $store_info;
				}
				break;

			// case 'store'
		}

		$struct = $this->config[$type]['struct'];

		$_this = $this;
		$json = preg_replace_callback("/\{\{\s*(.*?)\s*\}\}/",function($matched) use ($type,$source_data,&$_this){

			$index = $matched[1];
			$config = $_this->config[$type][$index];

			$arr = array();
			// foreach($source as $)

			foreach($source_data as $key => $data){
				$arr[$key] = array();
				foreach($config as $k => $v){
					$arr[$key][$k] = $_this->get_value($v,$data,$k);
				}
			}

			return json_encode($arr);
			

		},$struct);

		return $json;

	}

	function get_value($field,$data,$fname = null){
		if(is_array($field)){
			if(isset($field[0])){
				$ret = '';
				foreach($field as $key => $val){
					$ret .= $this->get_value($val,$data,$fname);
				}
				return $ret;
				// return implode($ret);
			}
			else{
				$exten = unserialize($data['goods']['gextends']);
				return $exten[$fname];
			}
		}
		else{
			if(strpos($field,'.') !== false){
				$arr = explode('.',$field);
				if($arr[0] == 'php'){
					return call_user_func($arr[1],array());
				}
				else{
					if(isset($data[$arr[0]])){
						return $data[$arr[0]][$arr[1]];
					}
					//如果没有这个属性，到common表里找
					elseif($arr[0] == 'goods'){
						$exten = Model()->table('goods_common')->where(array('goods_commonid' => $data['goods']['goods_commonid']))->find();
						if(isset($exten[$arr[1]])){
							return $exten[$arr[1]];
						}
					}
				}
			}
			else{
				return $field;
			}
		}
	}

	function file_get_contents_post($url, $post) {  
	    $options = array(  
	        'http' => array(  
	            'method' => 'POST',  
	            // 'content' => 'name=caiknife&email=caiknife@gmail.com',  
	            'content' => http_build_query($post),  
	        ),  
	    );  
	  
	    $result = file_get_contents($url, false, stream_context_create($options));  
	  
	    return $result;  
	}  
}

$limit_count = 0;
function getrandom(){
	global $limit_count;
	$count = ++$limit_count;
	$GLOBALS['limit_count'] = $count; 
	$count = (string)$count;
	for($i = 0; $i < 6 - strlen($count); $i++){
		$count = '0' . $count;
	}
	return $count;
}

function curl_post($url,$post_data){
	// $post_data = array();  
	// $post_data['clientname'] = "test08";  
	// $post_data['clientpasswd'] = "test08";  
	// $post_data['submit'] = "submit";  
	$o="";  
	foreach ($post_data as $k=>$v)  
	{  
	    $o.= "$k=".urlencode($v)."&";  
	}  
	$post_data=substr($o,0,-1);  
	$ch = curl_init();  
	curl_setopt($ch, CURLOPT_POST, 1);  
	curl_setopt($ch, CURLOPT_HEADER, 0);  
	curl_setopt($ch, CURLOPT_URL,$url);  
	//为了支持cookie  
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');  
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
	return $result = curl_exec($ch); 
}

